<?php
include_once "lib/config.php";
include_once "header.php";


if(isset($_POST['save'])&& $_POST['save']!="")
{
	$data= $_POST['User'];
    $data['dob']=convertDateToTime($data['dob']);
    if($_FILES['profile_photo']['name']!="")
    {
        include_once "thumb.php";
        if($_FILES['profile_photo']['type']=="image/jpeg" || $_FILES['profile_photo']['type']=="image/gif" || $_FILES['profile_photo']['type']=="image/png" || $_FILES['profile_photo']['type']=="image/jpg" || $_FILES['profile_photo']['type']=="image/JPEG" || $_FILES['profile_photo']['type']=="image/GIF" || $_FILES['profile_photo']['type']=="image/PNG" || $_FILES['profile_photo']['type']=="image/JPG")
        {
            list($width, $height) = getimagesize($_FILES['profile_photo']["tmp_name"]);
            $folder="uloaded_files";
            $folder_val="uloaded_files/profile_photo";
            $exact_folder_val="uloaded_files/profile_photo/".$_SESSION['u_id'];
            $extension_arr=explode("/",$_FILES['profile_photo']['type']);
            $extension=$extension_arr[1];
            
            $f=$_FILES['profile_photo']['name'];
            $path=$folder_val."/".$f;
            
            
            if(file_exists($folder) && is_dir($folder))
            {
                if(file_exists($folder_val) && is_dir($folder_val))
                {
                    if(file_exists($exact_folder_val) && is_dir($exact_folder_val))
                    {
                        
                    }
                    else
                    {
                        if(mkdir($exact_folder_val,0777))
                        {
                            //echo " ->  Directory Created..";
                        }
                    }
                }
                else
                {
                    if(mkdir($folder_val,0777))
                    {
                        //echo " ->  Directory Created..";
                    }
                    if(mkdir($exact_folder_val,0777))
                    {
                        //echo " ->  Directory Created..";
                    }
                }
                    
            }
            else
            {
                if(mkdir($folder,0777))
                {
                    //echo "--> outer folder created ..";
                }
                if(mkdir($folder_val,0777))
                {
                    //echo " ->  Directory Created..";
                }
                if(mkdir($exact_folder_val,0777))
                {
                    //echo " ->  Directory Created..";
                }
                
            }
            $target=$folder_val."/";
            
            $new_path=$exact_folder_val."/".$_FILES['profile_photo']['name'];
            
            if($width>140 || $height>196)
            {
                $move= move_uploaded_file($_FILES['profile_photo']['tmp_name'], $new_path);
                if($move)
                {
                    createThumbsforProfiles_widthTransparent($new_path,$new_path,140,196,"width");
                }
                
            }
            else
            {
                $move= move_uploaded_file($_FILES['profile_photo']['tmp_name'], $new_path);
            }
            $data['profile_photo']=$new_path;    
        }    
    }
	if(isset($_POST['staff_id']) && $_POST['staff_id']>0)
	{
	    $check_query=DB::updatecheckExistance("users",array("email_id"=>$data['email_id']),array("id"=>$_POST['staff_id']));
        
		if($check_query==0)
		{
		    $data['type_id']=1;
            $data['password_encrypted']=base64_encode(trim($data['password']));
            $data['password']=md5(trim($data['password']));
            DB::updateData("users",$data,array("id"=>$_POST['staff_id']));
				
			if(DB::$affectedrows>0)
			{
			    
				$_SESSION['msg_success']="Staff updated successfully.";
                redirect('staffs.php');
			}
			else
			{
				$msg_error="There is no record updated.";
			}
		}
		else
		{
			$msg_error="There is already one staff record exists for given email id.";
		}
	}
	else
	{
		$check_query=DB::checkExistance("users",array("email_id"=>$data['email_id']));
        
		if($check_query==0)
		{
			$data['type_id']=1;
            $data['password_encrypted']=base64_encode(trim($data['password']));
            $data['password']=md5(trim($data['password']));
            $data['status']=1;
			DB::insertData("users",$data);
			if(DB::$affectedrows>0)
			{
			 
				$_SESSION['msg_success']="Staff added successfully.";
				redirect('staffs.php');
			}
			else
			{
				$msg_error="There is no record added.";
			}
			
		}
		else
		{
			$msg_error="There is already one staff record exists for given email id.";
		}
	}
	
}
$dob=date("d/m/Y");
if(isset($_POST['staff_id']) && $_POST['staff_id']>0)
{
	$conditionarr=array(":id"=>$_POST['staff_id']);
    $staffinfo=DB::fetch_array(DB::query("select * from users where id=:id",$conditionarr));
    
    if(isset($staffinfo[0]))
    {
        $data=$staffinfo[0];
    }
    else
    {
        $data=array();
    }
    if($data['dob']>0)
    {
        $dob=date("d/m/Y",$data['dob']);    
    }
}

?>
    <link rel="stylesheet" href="css/jquery-ui.css" />
  <script src="js/jquery-1.9.1.js"></script>
  <script src="js/jquery-ui.js"></script>		
  <script>
  $(function() {
    $('#dob').datepicker({
		dateFormat: 'dd/mm/yy',
		changeMonth: true,
	    changeYear: true,
	    yearRange: '1950:<?php echo date("Y"); ?>',
	    
	    inline: true
		}).datepicker('setDate', '<?php echo $dob; ?>');
    
  });
  
  </script>
		<div class="container_12" id="main_div">
           <form action="" method="post" onsubmit="return validateaddeditstaff();" enctype="multipart/form-data">
			<br>
			<div class="grid_12" >
		    	<?php
            	if(isset($msg_success) && $msg_success!="")
            	{
            	?>
                <div>
                    <span class="notification n-success"><?php echo $msg_success; ?></span>
                </div>
                <?php
                }
                if(isset($msg_error) && $msg_error!="")
                {
                ?>
                <span class="notification n-error"><?php echo $msg_error; ?></span>
                <?php
				}
                ?>
		        <div class="module" >
		        		
		             <h2><span onclick="toggle_div('basic_body','toogleimg');" ><img src="images/desc.gif" height="5px" id="toogleimg">&nbsp;&nbsp;Staff Details</span></h2>
		                
		             <div class="module-body" id="basic_body">
		                
		                  	<p>
		                        <label>Full Name<span style="color: red;">*</span></label>
		                        <input type="text" class="input-short" style="width: 280px;" id="name" name="User[full_name]" value="<?php if(isset($data['full_name']) && $data['full_name']!=Null)echo $data['full_name']; ?>"/>
		                        
		                    </p>
		                    <p>
		                        <label>Email<span style="color: red;">*</span></label>
		                        <input type="text" name="User[email_id]" id="email_id" class="input-short" style="width: 280px;" value="<?php if(isset($data['email_id']) && $data['email_id']!=Null)echo $data['email_id']; ?>">
		                    </p>
                            <p>
		                        <label>Password<span style="color: red;">*</span></label>
		                        <input type="password" name="User[password]" id="password" class="input-short" style="width: 280px;" value="<?php if(isset($data['password_encrypted']) && $data['password_encrypted']!=Null){ echo base64_decode($data['password_encrypted']); }else{ if(isset($data['password'])){ echo $data['password']; }} ?>"/>
		                    </p>
                            <p>
		                        <label>Confirm Password<span style="color: red;">*</span></label>
		                        <input type="password" id="cpassword" class="input-short" style="width: 280px;" value="<?php if(isset($data['password_encrypted']) && $data['password_encrypted']!=Null){ echo base64_decode($data['password_encrypted']); }else{ if(isset($data['password'])){ echo $data['password']; }} ?>" />
		                    </p>
		                    <p>
		                        <label>Gender<span style="color: red;">*</span></label>
		                        <select class="input-short" id="gender" name="User[gender]" style="width: 280px;">
		                            <option value="">Select Gender</option>
		                            <option value="Male" <?php if(isset($data['gender']) && $data['gender']!=Null){ if($data['gender']=="Male")echo "selected"; } ?>>Male</option>
		                            <option value="Female" <?php if(isset($data['gender']) && $data['gender']!=Null){ if($data['gender']=="Female")echo "selected"; } ?>>Female</option>
		                        </select>
		                    </p>
		                    
		                    
		                    <p>
		                        <label>Date of Birth<span style="color: red;">*</span></label>
		                        <input type="text" name="User[dob]" value="" id="dob" readonly="true" class="input-short" style="width: 280px;">
		                    </p>
		                    
		                    <p>
		                        <label>Profile Picture</label>
		                        <input type="file" name="profile_photo" id="profile_photo" class="input-short" style="width: 280px;">
                                <input type="hidden" name="User[profile_photo]" value="<?php if(isset($data['profile_photo']) && $data['profile_photo']!=Null)echo $data['profile_photo']; ?>" />
		                    </p>
		                    <p>
		                        <label>Contact Number<span style="color: red;">*</span></label>
		                        <input type="text" name="User[contact_number]" id="contact_number" class="input-short" style="width: 280px;" value="<?php if(isset($data['contact_number']) && $data['contact_number']!=Null)echo $data['contact_number']; ?>" onkeypress="return isNumberKey(event)">
		                    </p>
		                    <p>
		                        <label>Address<span style="color: red;">*</span></label>
		                        <textarea rows="7" cols="90" name="User[address]" id="address" class="input-short"><?php if(isset($data['address']) && $data['address']!=Null)echo $data['address']; ?></textarea>
		                    </p>
		                    <p>
		                        <label>City<span style="color: red;">*</span></label>
		                        <input type="text" name="User[city]" id="city" class="input-short" style="width: 280px;" value="<?php if(isset($data['city']) && $data['city']!=Null)echo $data['city']; ?>">
		                    </p>
		                    <p>
		                        <label>State<span style="color: red;">*</span></label>
		                        <input type="text" name="User[state]" id="state" class="input-short" style="width: 280px;" value="<?php if(isset($data['state']) && $data['state']!=Null)echo $data['state']; ?>">
		                    </p>
                            <p>
		                        <label>Country<span style="color: red;">*</span></label>
		                        <input type="text" name="User[country]" id="country" class="input-short" style="width: 280px;" value="<?php if(isset($data['country']) && $data['country']!=Null)echo $data['country']; ?>">
		                    </p>
		                    <p>
		                        <label>Pin<span style="color: red;">*</span></label>
		                        <input type="text" name="User[pincode]" id="pincode" class="input-short" style="width: 280px;" value="<?php if(isset($data['pincode']) && $data['pincode']!=Null)echo $data['pincode']; ?>" >
		                    </p>
		                
		             </div> <!-- End .module-body -->
		
		        </div>  <!-- End .module -->
				
		    </div>
		    
		    <div class="grid_12">
		    <fieldset>
		    	&nbsp;
		    	<input type="hidden" id="staff_id" name="staff_id" value="<?php if(isset($_POST['staff_id'])){ echo $_POST['staff_id']; } ?>">
		        <input class="submit-green" type="submit" name="save" value="Submit" /> 
		        <input class="submit-gray" type="button" value="Back" onclick="window.location='staffs.php'" />
		    </fieldset>
		    </div>
		    <div style="clear:both;"></div>
		 </form>    
        </div> <!-- End .container_12 -->
		
<?php include_once("footer.php"); ?>           
<?php
if(isset($msg_success) && $msg_success!="")
{
	$notymessage=$msg_success;
	$msgtype="success";
}
else if(isset($msg_error) && $msg_error!="")
{
	$msgtype="error";
	$notymessage=$msg_error;
}
else
{
	$msgtype="";
}
if($msgtype!="")
{
?>        
<script language="javascript">
	$(document).ready(function(){
		
		var options = $.parseJSON('{"text":"<?php echo $notymessage; ?>","layout":"top","type":"<?php echo $msgtype; ?>"}');
		noty(options);
		
	});
</script>  
<?php
}
?>
<script>

function validateaddeditstaff()
{
	
	if(trim(document.getElementById("name").value)=="")
	{
		alertify.alert("Name field required!");
		document.getElementById("name").focus();
		return false;
	}
	else if(trim(document.getElementById("email_id").value)=="")
	{
		alertify.alert("Email field required!");
		document.getElementById("email_id").focus();
		return false;
	}
    else if(document.getElementById("password").value=="")
	{
		alertify.alert("Password field is required.");
		document.getElementById("password").focus();
		return false;
	}
	else if(document.getElementById("password").value.length<6)
	{
		alertify.alert("Password should be at least 6 characters long!");
		document.getElementById("password").focus();
		return false;
	}
	else if(trim(document.getElementById("password").value)!=trim(document.getElementById("cpassword").value))
	{
		alertify.alert("Confirm password mismatched!");
		document.getElementById("cpassword").focus();
		return false;
	}
	else if(trim(document.getElementById("gender").value)=="")
	{
		alertify.alert("Gender field required!");
		document.getElementById("gender").focus();
		return false;
	}
	else if(trim(document.getElementById("dob").value)=="")
	{
		alertify.alert("Date of birth field required!");
		document.getElementById("dob").focus();
		return false;
	}
	else if(trim(document.getElementById("contact_number").value)=="")
	{
		alertify.alert("Contact Number field required!");
		document.getElementById("contact_number").focus();
		return false;
	}
	else if(trim(document.getElementById("address").value)=="")
	{
		alertify.alert("Address field required!");
		document.getElementById("address").focus();
		return false;
	}
	else if(trim(document.getElementById("city").value)=="")
	{
		alertify.alert("City field required!");
		document.getElementById("city").focus();
		return false;
	}
	else if(trim(document.getElementById("state").value)=="")
	{
		alertify.alert("State field required!");
		document.getElementById("state").focus();
		return false;
	}
    else if(trim(document.getElementById("country").value)=="")
	{
		alertify.alert("Country field required!");
		document.getElementById("country").focus();
		return false;
	}
    else if(trim(document.getElementById("pincode").value)=="")
	{
		alertify.alert("Pin Code field required!");
		document.getElementById("pincode").focus();
		return false;
	}
}
</script>       